|
|
|
|
|
|
|
1. Display the current image. |
|
|
|
|
|
|
|
|
2. Generate the next image. |
|
|
|
|
|
|
|
|
3. See how much time remains before you need to present the next image, and set the interval property accordingly. |
|
|
|
|
|
|
|
|
All this makes an otherwise simple process annoyingly complex. |
|
|
|
|
|
|
|
|
Another approach to timing frames is to keep checking the clock until it is time to display the next image. The steps you follow are very similar to those for resetting a timer control's Interval property: |
|
|
|
|
|
|
|
|
1. Display the current image. |
|
|
|
|
|
|
|
|
2. Generate the next image. |
|
|
|
|
|
|
|
|
3. See how much time remains before you need to present the next image. Use a Do While loop to wait until that time arrives. |
|
|
|
|
|
|
|
|
You can use Visual Basic's Timer function to determine the current time, but the API function GetTickCount is faster and more accurate. GetTickCount returns the number of milliseconds that have elapsed since you started Windows. The WaitTill subroutine shown in the following code waits until a specified time arrives. |
|
|
|
 |
|
|
|
|
Declare Function GetTickCount Lib "kernel32" () As Long
' Pause until GetTickCount shows the indicated time. Public Sub WaitTill(next_time As Long)
Do
DoEvents
Loop While GetTickCount() < next_time
End Sub |
|
|
|
|
|
|
|
|
Subroutine WaitTill uses a Do While loop to wait until the indicated time. Each time through the loop, the routine executes the Visual Basic DoEvents statement so that Windows can process events for this program and others. Note that the test to end this loop occurs at the end, so DoEvents is executed at least once. |
|
|
|
|
|
|
|
|
Because WaitTill uses DoEvents, the program is at the mercy of the operating system to a certain extent. There is no easy way to tell ahead of time how long a call to DoEvents will take. If other programs are using the CPU heavily, DoEvents may take a while, making WaitTill less accurate. |
|
|
|
|
|
|
|
|
The following code shows one way a program could use WaitTill to animate a series of frames. |
|
|
|
 |
|
|
|
|
' Run the animation once or until Playing is False. Private Sub PlayImagesOnce(ByVal ms_per_frame As Integer) |
|
|
|
|
|